package com.eye.catcher.service.generate.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eye.catcher.common.RedisUtils;
import com.eye.catcher.constants.RedisConstants;
import com.eye.catcher.entity.TSsDaStatisticsInfo;
import com.eye.catcher.entity.TYwDaFieldInfo;
import com.eye.catcher.mapper.TSsDaStatisticsInfoMapper;
import com.eye.catcher.service.generate.ITSsDaStatisticsInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eye.catcher.system.role.param.RoleDto;
import com.eye.catcher.system.user.param.UserLoginInfo;
import com.eye.catcher.system.yw.statistics.param.ObserveHasCreateInfoDto;
import com.eye.catcher.system.yw.statistics.param.ObserveHasCreateInfoPage;
import com.eye.catcher.system.yw.statistics.param.StatisticsInfoDto;
import com.eye.catcher.system.yw.statistics.param.StatisticsInfoPage;
import com.eye.catcher.system.yw.statistics.vo.ObserveHasCreateInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huzhiwei
 * @since 2022-08-03
 */
@Service
public class TSsDaStatisticsInfoServiceImpl extends ServiceImpl<TSsDaStatisticsInfoMapper, TSsDaStatisticsInfo> implements ITSsDaStatisticsInfoService {

    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private TSsDaStatisticsInfoMapper statisticsInfoMapper;
    @Value("${base.all-permissions.roleId}")
    private String baseAllPermissionsRoleId;

    @Override
    public Object getStatisticsPageList(StatisticsInfoPage statisticsInfoPage) {
        Page<TSsDaStatisticsInfo> page=new Page<>(statisticsInfoPage.getPage().getPageNum(),statisticsInfoPage.getPage().getPageSize());
        QueryWrapper<TSsDaStatisticsInfo> wrapper = Wrappers.query();
        //组装查询条件
        this.appendWrapper(wrapper,statisticsInfoPage.getStatisticsInfo());

        //list
        IPage<TSsDaStatisticsInfo> returnPage = statisticsInfoMapper.statisticsPage(page, wrapper);
        //总数
        returnPage.setTotal(statisticsInfoMapper.statisticsCount(wrapper));
        return returnPage;
    }

    @Override
    public String exportStatistics(StatisticsInfoDto statisticsInfo, HttpServletRequest request, HttpServletResponse response) {
        QueryWrapper<TSsDaStatisticsInfo> wrapper = Wrappers.query();
        //组装查询条件
        this.appendWrapper(wrapper,statisticsInfo);
        List<TSsDaStatisticsInfo> list = statisticsInfoMapper.statisticsList(wrapper);
        Iterator it= list.iterator();
        ByteArrayOutputStream baos=null;
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            String s = sdf.format(new Date());
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("观测期统计");
            XSSFRow row = sheet.createRow((short) 0);
            XSSFCell cell = null;
            cell = row.createCell((short) 0);
            cell.setCellValue("序号");
            cell = row.createCell((short) 1);
            cell.setCellValue("基地名称");
            sheet.setColumnWidth(1, 25*256);
            cell = row.createCell((short) 2);
            cell.setCellValue("观测期");
            sheet.setColumnWidth(2, 25*256);
            cell = row.createCell((short) 3);
            cell.setCellValue("年度");
            sheet.setColumnWidth(3, 25*256);
            cell = row.createCell((short) 4);
            cell.setCellValue("基地类型");
            sheet.setColumnWidth(4, 25*256);
            cell = row.createCell((short) 5);
            cell.setCellValue("主茎叶龄");
            sheet.setColumnWidth(5, 25*256);
            cell = row.createCell((short) 6);
            cell.setCellValue("单株一级分糵");
            sheet.setColumnWidth(6, 25*256);
            cell = row.createCell((short) 7);
            cell.setCellValue("茎糵数");
            sheet.setColumnWidth(7, 25*256);
            cell = row.createCell((short) 8);
            cell.setCellValue("单株次生根条数");
            sheet.setColumnWidth(8, 25*256);

//            Object obj = redisUtils.hget("t_cod_ada_dictionary", "t_cod_da_file_status");
            int i=0;
            while(it.hasNext()){
                TSsDaStatisticsInfo info=(TSsDaStatisticsInfo)it.next();

                row=sheet.createRow((int)i+1);
                row.createCell((short) 0).setCellValue(i + 1);
                this.setCellValue(row,1,info.getBaseName());
                this.setCellValue(row,2,info.getObserveName());
                this.setCellValue(row,3,""+info.getAnnual());
                this.setCellValue(row,4,info.getType());
                this.setCellValue(row,5,info.getMainStemLeafAge());
                this.setCellValue(row,6,info.getIndividualLevel());
                this.setCellValue(row,7,info.getStemsNumber());
                this.setCellValue(row,8,info.getSinglePlantDefectiveNumber());
                i++;
            }
            String filename = "观测期统计" + s + ".xlsx";
            baos = new ByteArrayOutputStream();
            workbook.write(baos);
            byte[] getData = baos.toByteArray();
            baos.write(getData);
            if(getData.length != 0){
                response.setCharacterEncoding("utf-8");
                response.setContentType("application/x-msdownload;charset=utf-8");
                response.setContentLength(getData.length);
                response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(filename,"UTF-8"));
                response.getOutputStream().write(getData,0,getData.length);
            }else{
                response.setHeader("MsgError","404");
            }
            response.getOutputStream().flush();
            response.getOutputStream().close();

        }
        catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(null != baos){
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return null;
    }

    @Override
    public Object getPlantingStatisticsPageList(StatisticsInfoPage statisticsInfoPage) {
        Page<TSsDaStatisticsInfo> page=new Page<>(statisticsInfoPage.getPage().getPageNum(),statisticsInfoPage.getPage().getPageSize());
        QueryWrapper<TSsDaStatisticsInfo> wrapper = Wrappers.query();
        //组装查询条件
        this.appendWrapper(wrapper,statisticsInfoPage.getStatisticsInfo());

        //list
        IPage<TSsDaStatisticsInfo> returnPage = statisticsInfoMapper.plantingStatisticsPage(page, wrapper);
        //总数
        returnPage.setTotal(statisticsInfoMapper.plantingStatisticsCount(wrapper));
        return returnPage;
    }
    @Override
    public String exportPlantingStatistics(StatisticsInfoDto statisticsInfo, HttpServletRequest request, HttpServletResponse response) {
        QueryWrapper<TSsDaStatisticsInfo> wrapper = Wrappers.query();
        //组装查询条件
        this.appendWrapper(wrapper,statisticsInfo);
        List<TSsDaStatisticsInfo> list = statisticsInfoMapper.plantingStatisticsList(wrapper);
        Iterator it= list.iterator();
        ByteArrayOutputStream baos=null;
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            String s = sdf.format(new Date());
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("城市种植面积统计");
            XSSFRow row = sheet.createRow((short) 0);
            XSSFCell cell = null;
            cell = row.createCell((short) 0);
            cell.setCellValue("序号");
            cell = row.createCell((short) 1);
            cell.setCellValue("城市");
            sheet.setColumnWidth(1, 25*256);
            cell = row.createCell((short) 2);
            cell.setCellValue("区县");
            sheet.setColumnWidth(2, 25*256);
            cell = row.createCell((short) 3);
            cell.setCellValue("观测期");
            sheet.setColumnWidth(3, 25*256);
            cell = row.createCell((short) 4);
            cell.setCellValue("年度");
            sheet.setColumnWidth(4, 25*256);
            cell = row.createCell((short) 5);
            cell.setCellValue("主茎叶龄");
            sheet.setColumnWidth(5, 25*256);
            cell = row.createCell((short) 6);
            cell.setCellValue("单株一级分糵");
            sheet.setColumnWidth(6, 25*256);
            cell = row.createCell((short) 7);
            cell.setCellValue("茎糵数");
            sheet.setColumnWidth(7, 25*256);
            cell = row.createCell((short) 8);
            cell.setCellValue("单株次生根条数");
            sheet.setColumnWidth(8, 25*256);

//            Object obj = redisUtils.hget("t_cod_ada_dictionary", "t_cod_da_file_status");
            int i=0;
            while(it.hasNext()){
                TSsDaStatisticsInfo info=(TSsDaStatisticsInfo)it.next();

                row=sheet.createRow((int)i+1);
                row.createCell((short) 0).setCellValue(i + 1);
                this.setCellValue(row,1,info.getCityName());
                this.setCellValue(row,2,info.getSubordinateCounties());
                this.setCellValue(row,3,info.getObserveName());
                this.setCellValue(row,4,""+info.getAnnual());
                this.setCellValue(row,5,info.getMainStemLeafAge());
                this.setCellValue(row,6,info.getIndividualLevel());
                this.setCellValue(row,7,info.getStemsNumber());
                this.setCellValue(row,8,info.getSinglePlantDefectiveNumber());i++;
            }
            String filename = "城市种植面积统计" + s + ".xlsx";
            baos = new ByteArrayOutputStream();
            workbook.write(baos);
            byte[] getData = baos.toByteArray();
            baos.write(getData);
            if(getData.length != 0){
                response.setCharacterEncoding("utf-8");
                response.setContentType("application/x-msdownload;charset=utf-8");
                response.setContentLength(getData.length);
                response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(filename,"UTF-8"));
                response.getOutputStream().write(getData,0,getData.length);
            }else{
                response.setHeader("MsgError","404");
            }
            response.getOutputStream().flush();
            response.getOutputStream().close();

        }
        catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(null != baos){
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return null;
    }

    @Override
    public String exportPlantingTypeStatistics(StatisticsInfoDto statisticsInfo, HttpServletRequest request, HttpServletResponse response) {
        QueryWrapper<TSsDaStatisticsInfo> wrapper = Wrappers.query();
        //组装查询条件
        this.appendWrapper(wrapper,statisticsInfo);
        List<TSsDaStatisticsInfo> list = statisticsInfoMapper.plantingTypeStatisticsList(wrapper);
        Iterator it= list.iterator();
        ByteArrayOutputStream baos=null;
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            String s = sdf.format(new Date());
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("城市种植面积基地类型统计");
            XSSFRow row = sheet.createRow((short) 0);
            XSSFCell cell = null;
            cell = row.createCell((short) 0);
            cell.setCellValue("序号");
            cell = row.createCell((short) 1);
            cell.setCellValue("城市");
            sheet.setColumnWidth(1, 25*256);
            cell = row.createCell((short) 2);
            cell.setCellValue("区县");
            sheet.setColumnWidth(2, 25*256);
            cell = row.createCell((short) 3);
            cell.setCellValue("观测期");
            sheet.setColumnWidth(3, 25*256);
            cell = row.createCell((short) 4);
            cell.setCellValue("年度");
            sheet.setColumnWidth(4, 25*256);
            cell = row.createCell((short) 5);
            cell.setCellValue("基地类型");
            sheet.setColumnWidth(5, 25*256);
            cell = row.createCell((short) 6);
            cell.setCellValue("主茎叶龄");
            sheet.setColumnWidth(6, 25*256);
            cell = row.createCell((short) 7);
            cell.setCellValue("单株一级分糵");
            sheet.setColumnWidth(7, 25*256);
            cell = row.createCell((short) 8);
            cell.setCellValue("茎糵数");
            sheet.setColumnWidth(8, 25*256);
            cell = row.createCell((short) 9);
            cell.setCellValue("单株次生根条数");
            sheet.setColumnWidth(9, 25*256);

//            Object obj = redisUtils.hget("t_cod_ada_dictionary", "t_cod_da_file_status");
            int i=0;
            while(it.hasNext()){
                TSsDaStatisticsInfo info=(TSsDaStatisticsInfo)it.next();

                row=sheet.createRow((int)i+1);
                row.createCell((short) 0).setCellValue(i + 1);
                this.setCellValue(row,1,info.getCityName());
                this.setCellValue(row,2,info.getSubordinateCounties());
                this.setCellValue(row,3,info.getObserveName());
                this.setCellValue(row,4,""+info.getAnnual());
                this.setCellValue(row,5,info.getType());
                this.setCellValue(row,6,info.getMainStemLeafAge());
                this.setCellValue(row,7,info.getIndividualLevel());
                this.setCellValue(row,8,info.getStemsNumber());
                this.setCellValue(row,9,info.getSinglePlantDefectiveNumber());i++;
            }
            String filename = "城市种植面积基地类型统计" + s + ".xlsx";
            baos = new ByteArrayOutputStream();
            workbook.write(baos);
            byte[] getData = baos.toByteArray();
            baos.write(getData);
            if(getData.length != 0){
                response.setCharacterEncoding("utf-8");
                response.setContentType("application/x-msdownload;charset=utf-8");
                response.setContentLength(getData.length);
                response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(filename,"UTF-8"));
                response.getOutputStream().write(getData,0,getData.length);
            }else{
                response.setHeader("MsgError","404");
            }
            response.getOutputStream().flush();
            response.getOutputStream().close();

        }
        catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(null != baos){
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return null;
    }

    @Override
    public Object getPlantingTypeStatisticsPageList(StatisticsInfoPage statisticsInfoPage) {
        Page<TSsDaStatisticsInfo> page=new Page<>(statisticsInfoPage.getPage().getPageNum(),statisticsInfoPage.getPage().getPageSize());
        QueryWrapper<TSsDaStatisticsInfo> wrapper = Wrappers.query();
        //组装查询条件
        this.appendWrapper(wrapper,statisticsInfoPage.getStatisticsInfo());

        //list
        IPage<TSsDaStatisticsInfo> returnPage = statisticsInfoMapper.plantingTypeStatisticsPage(page, wrapper);
        //总数
        returnPage.setTotal(statisticsInfoMapper.plantingTypeStatisticsCount(wrapper));
        return returnPage;
    }

    @Override
    public Object getRegionStatisticsPageList(StatisticsInfoPage statisticsInfoPage) {
        Page<TSsDaStatisticsInfo> page=new Page<>(statisticsInfoPage.getPage().getPageNum(),statisticsInfoPage.getPage().getPageSize());
        QueryWrapper<TSsDaStatisticsInfo> wrapper = Wrappers.query();
        //组装查询条件
        this.appendWrapper(wrapper,statisticsInfoPage.getStatisticsInfo());

        //list
        IPage<TSsDaStatisticsInfo> returnPage = statisticsInfoMapper.regionStatisticsPage(page, wrapper);
        //总数
        returnPage.setTotal(statisticsInfoMapper.regionStatisticsCount(wrapper));
        return returnPage;
    }


    @Override
    public String exportRegionStatistics(StatisticsInfoDto statisticsInfo, HttpServletRequest request, HttpServletResponse response) {
        QueryWrapper<TSsDaStatisticsInfo> wrapper = Wrappers.query();
        //组装查询条件
        this.appendWrapper(wrapper,statisticsInfo);
        List<TSsDaStatisticsInfo> list = statisticsInfoMapper.regionStatisticsList(wrapper);
        Iterator it= list.iterator();
        ByteArrayOutputStream baos=null;
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            String s = sdf.format(new Date());
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("地区统计");
            XSSFRow row = sheet.createRow((short) 0);
            XSSFCell cell = null;
            cell = row.createCell((short) 0);
            cell.setCellValue("序号");
            cell = row.createCell((short) 1);
            cell.setCellValue("地区");
            sheet.setColumnWidth(1, 25*256);
            cell = row.createCell((short) 2);
            cell.setCellValue("观测期");
            sheet.setColumnWidth(2, 25*256);
            cell = row.createCell((short) 3);
            cell.setCellValue("年度");
            sheet.setColumnWidth(3, 25*256);
            cell = row.createCell((short) 4);
            cell.setCellValue("主茎叶龄");
            sheet.setColumnWidth(4, 25*256);
            cell = row.createCell((short) 5);
            cell.setCellValue("单株一级分糵");
            sheet.setColumnWidth(5, 25*256);
            cell = row.createCell((short) 6);
            cell.setCellValue("茎糵数");
            sheet.setColumnWidth(6, 25*256);
            cell = row.createCell((short) 7);
            cell.setCellValue("单株次生根条数");
            sheet.setColumnWidth(7, 25*256);

//            Object obj = redisUtils.hget("t_cod_ada_dictionary", "t_cod_da_file_status");
            int i=0;
            while(it.hasNext()){
                TSsDaStatisticsInfo info=(TSsDaStatisticsInfo)it.next();

                row=sheet.createRow((int)i+1);
                row.createCell((short) 0).setCellValue(i + 1);
                this.setCellValue(row,1,info.getRegion());
                this.setCellValue(row,2,info.getObserveName());
                this.setCellValue(row,3,""+info.getAnnual());
                this.setCellValue(row,4,info.getMainStemLeafAge());
                this.setCellValue(row,5,info.getIndividualLevel());
                this.setCellValue(row,6,info.getStemsNumber());
                this.setCellValue(row,7,info.getSinglePlantDefectiveNumber());i++;
            }
            String filename = "地区统计" + s + ".xlsx";
            baos = new ByteArrayOutputStream();
            workbook.write(baos);
            byte[] getData = baos.toByteArray();
            baos.write(getData);
            if(getData.length != 0){
                response.setCharacterEncoding("utf-8");
                response.setContentType("application/x-msdownload;charset=utf-8");
                response.setContentLength(getData.length);
                response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(filename,"UTF-8"));
                response.getOutputStream().write(getData,0,getData.length);
            }else{
                response.setHeader("MsgError","404");
            }
            response.getOutputStream().flush();
            response.getOutputStream().close();

        }
        catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(null != baos){
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return null;
    }



    @Override
    public Object getRegionTypeStatisticsPageList(StatisticsInfoPage statisticsInfoPage) {
        Page<TSsDaStatisticsInfo> page=new Page<>(statisticsInfoPage.getPage().getPageNum(),statisticsInfoPage.getPage().getPageSize());
        QueryWrapper<TSsDaStatisticsInfo> wrapper = Wrappers.query();
        //组装查询条件
        this.appendWrapper(wrapper,statisticsInfoPage.getStatisticsInfo());

        //list
        IPage<TSsDaStatisticsInfo> returnPage = statisticsInfoMapper.regionTypeStatisticsPage(page, wrapper);
        //总数
        returnPage.setTotal(statisticsInfoMapper.regionTypeStatisticsCount(wrapper));
        return returnPage;
    }

    @Override
    public String exportRegionTypeStatistics(StatisticsInfoDto statisticsInfo, HttpServletRequest request, HttpServletResponse response) {
        QueryWrapper<TSsDaStatisticsInfo> wrapper = Wrappers.query();
        //组装查询条件
        this.appendWrapper(wrapper,statisticsInfo);
        List<TSsDaStatisticsInfo> list = statisticsInfoMapper.regionTypeStatisticsList(wrapper);
        Iterator it= list.iterator();
        ByteArrayOutputStream baos=null;
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            String s = sdf.format(new Date());
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("地区基地类型统计");
            XSSFRow row = sheet.createRow((short) 0);
            XSSFCell cell = null;
            cell = row.createCell((short) 0);
            cell.setCellValue("序号");
            cell = row.createCell((short) 1);
            cell.setCellValue("地区");
            sheet.setColumnWidth(1, 25*256);
            cell = row.createCell((short) 2);
            cell.setCellValue("观测期");
            sheet.setColumnWidth(2, 25*256);
            cell = row.createCell((short) 3);
            cell.setCellValue("年度");
            sheet.setColumnWidth(3, 25*256);
            cell = row.createCell((short) 4);
            cell.setCellValue("基地类型");
            sheet.setColumnWidth(4, 25*256);
            cell = row.createCell((short) 5);
            cell.setCellValue("主茎叶龄");
            sheet.setColumnWidth(5, 25*256);
            cell = row.createCell((short) 6);
            cell.setCellValue("单株一级分糵");
            sheet.setColumnWidth(6, 25*256);
            cell = row.createCell((short) 7);
            cell.setCellValue("茎糵数");
            sheet.setColumnWidth(7, 25*256);
            cell = row.createCell((short) 8);
            cell.setCellValue("单株次生根条数");
            sheet.setColumnWidth(8, 25*256);

//            Object obj = redisUtils.hget("t_cod_ada_dictionary", "t_cod_da_file_status");
            int i=0;
            while(it.hasNext()){
                TSsDaStatisticsInfo info=(TSsDaStatisticsInfo)it.next();

                row=sheet.createRow((int)i+1);
                row.createCell((short) 0).setCellValue(i + 1);
                this.setCellValue(row,1,info.getRegion());
                this.setCellValue(row,2,info.getObserveName());
                this.setCellValue(row,3,""+info.getAnnual());
                this.setCellValue(row,4,info.getType());
                this.setCellValue(row,5,info.getMainStemLeafAge());
                this.setCellValue(row,6,info.getIndividualLevel());
                this.setCellValue(row,7,info.getStemsNumber());
                this.setCellValue(row,8,info.getSinglePlantDefectiveNumber());i++;
            }
            String filename = "地区基地类型统计" + s + ".xlsx";
            baos = new ByteArrayOutputStream();
            workbook.write(baos);
            byte[] getData = baos.toByteArray();
            baos.write(getData);
            if(getData.length != 0){
                response.setCharacterEncoding("utf-8");
                response.setContentType("application/x-msdownload;charset=utf-8");
                response.setContentLength(getData.length);
                response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(filename,"UTF-8"));
                response.getOutputStream().write(getData,0,getData.length);
            }else{
                response.setHeader("MsgError","404");
            }
            response.getOutputStream().flush();
            response.getOutputStream().close();

        }
        catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(null != baos){
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return null;
    }

    private void appendWrapper(QueryWrapper<TSsDaStatisticsInfo> wrapper, StatisticsInfoDto statisticsInfo) {
        if(StringUtils.isNotEmpty(statisticsInfo.getAnnual())){
            wrapper.eq("annual",statisticsInfo.getAnnual());
        }
        if(StringUtils.isNotEmpty(statisticsInfo.getType())){
            wrapper.eq("type",statisticsInfo.getType());
        }
        if(StringUtils.isNotEmpty(statisticsInfo.getCityName())){
            wrapper.eq("cityName",statisticsInfo.getCityName());
        }
        if(StringUtils.isNotEmpty(statisticsInfo.getSubordinateCounties())){
            wrapper.eq("subordinateCounties",statisticsInfo.getSubordinateCounties());
        }
        if(StringUtils.isNotEmpty(statisticsInfo.getRegion())){
            wrapper.eq("region",statisticsInfo.getRegion());
        }
        if(StringUtils.isNotEmpty(statisticsInfo.getBaseName())){
            wrapper.like("BASE_NAME",statisticsInfo.getBaseName());
        }
        if(StringUtils.isNotEmpty(statisticsInfo.getBaseId())){
            wrapper.eq("BASE_ID",statisticsInfo.getBaseId());
        }
        if(StringUtils.isNotEmpty(statisticsInfo.getObserveName())){
            wrapper.like("OBSERVE_NAME",statisticsInfo.getObserveName());
        }
        if(StringUtils.isNotEmpty(statisticsInfo.getUserId())){
            UserLoginInfo userLoginInfo = (UserLoginInfo)redisUtils.get(RedisConstants.USER_INFO_EXPIRE.getKey()+statisticsInfo.getUserId());
            if(!"admin".equals(userLoginInfo.getUserDto().getLoginName())){
                Set<String> rolesSet =new HashSet<>(
                        userLoginInfo.getRoleDtoList().stream().map(RoleDto::getId).collect(Collectors.toList())
                );
                if(!rolesSet.contains(baseAllPermissionsRoleId)){
                    wrapper.eq("BASE_ID",userLoginInfo.getUserDto().getBaseId());
                }
            }
        }
    }



    @Override
    public Object getObserveHasCreatePageList(ObserveHasCreateInfoPage observeHasCreateInfoPage) {
        Page<ObserveHasCreateInfoVo> page=new Page<>(observeHasCreateInfoPage.getPage().getPageNum(),observeHasCreateInfoPage.getPage().getPageSize());
        QueryWrapper<ObserveHasCreateInfoVo> wrapper = Wrappers.query();
        //组装查询条件
        this.appendObWrapper(wrapper,observeHasCreateInfoPage.getObserveHasCreateInfo());

        //list
        IPage<ObserveHasCreateInfoVo> returnPage = statisticsInfoMapper.observeHasCreatePage(page, wrapper);
        //总数
        returnPage.setTotal(statisticsInfoMapper.observeHasCreateCount(wrapper));
        return returnPage;
    }

    @Override
    public String exportObserveHasCreateStatistics(ObserveHasCreateInfoDto observeHasCreateInfo, HttpServletRequest request, HttpServletResponse response) {
        QueryWrapper<ObserveHasCreateInfoVo> wrapper = Wrappers.query();
        //组装查询条件
        this.appendObWrapper(wrapper,observeHasCreateInfo);
        List<ObserveHasCreateInfoVo> list = statisticsInfoMapper.observeHasCreateList(wrapper);
        Iterator it= list.iterator();
        ByteArrayOutputStream baos=null;
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            String s = sdf.format(new Date());
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("未填观测期统计");
            XSSFRow row = sheet.createRow((short) 0);
            XSSFCell cell = null;
            cell = row.createCell((short) 0);
            cell.setCellValue("序号");
            cell = row.createCell((short) 1);
            cell.setCellValue("基地");
            sheet.setColumnWidth(1, 25*256);
            cell = row.createCell((short) 2);
            cell.setCellValue("田地");
            sheet.setColumnWidth(2, 25*256);
            cell = row.createCell((short) 3);
            cell.setCellValue("观测期");
            sheet.setColumnWidth(3, 25*256);
            cell = row.createCell((short) 4);
            cell.setCellValue("数量");
            sheet.setColumnWidth(4, 25*256);

//            Object obj = redisUtils.hget("t_cod_ada_dictionary", "t_cod_da_file_status");
            int i=0;
            while(it.hasNext()){
                ObserveHasCreateInfoVo info=(ObserveHasCreateInfoVo)it.next();

                row=sheet.createRow((int)i+1);
                row.createCell((short) 0).setCellValue(i + 1);
                this.setCellValue(row,1,info.getBaseName());
                this.setCellValue(row,2,info.getFieldName());
                this.setCellValue(row,3,info.getObserveName());
                this.setCellValue(row,4,info.getCount());
                i++;
            }
            String filename = "未填观测期统计" + s + ".xlsx";
            baos = new ByteArrayOutputStream();
            workbook.write(baos);
            byte[] getData = baos.toByteArray();
            baos.write(getData);
            if(getData.length != 0){
                response.setCharacterEncoding("utf-8");
                response.setContentType("application/x-msdownload;charset=utf-8");
                response.setContentLength(getData.length);
                response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(filename,"UTF-8"));
                response.getOutputStream().write(getData,0,getData.length);
            }else{
                response.setHeader("MsgError","404");
            }
            response.getOutputStream().flush();
            response.getOutputStream().close();

        }
        catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(null != baos){
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return null;
    }

    private void appendObWrapper(QueryWrapper<ObserveHasCreateInfoVo> wrapper, ObserveHasCreateInfoDto observeHasCreateInfo) {
        if(StringUtils.isNotEmpty(observeHasCreateInfo.getBaseId())){
            wrapper.eq("baseId",observeHasCreateInfo.getBaseId());
        }
        if(StringUtils.isNotEmpty(observeHasCreateInfo.getBaseCode())){
            wrapper.eq("baseCode",observeHasCreateInfo.getBaseCode());
        }
        if(StringUtils.isNotEmpty(observeHasCreateInfo.getBaseName())){
            wrapper.like("baseName",observeHasCreateInfo.getBaseName());
        }
        if(StringUtils.isNotEmpty(observeHasCreateInfo.getFieldId())){
            wrapper.eq("fieldId",observeHasCreateInfo.getFieldId());
        }
        if(StringUtils.isNotEmpty(observeHasCreateInfo.getFieldCode())){
            wrapper.eq("fieldCode",observeHasCreateInfo.getFieldCode());
        }
        if(StringUtils.isNotEmpty(observeHasCreateInfo.getFieldName())){
            wrapper.like("fieldName",observeHasCreateInfo.getFieldName());
        }
        if(StringUtils.isNotEmpty(observeHasCreateInfo.getObserveName())){
            wrapper.eq("observeName",observeHasCreateInfo.getObserveName());
        }
        if(StringUtils.isNotEmpty(observeHasCreateInfo.getUserId())){
            UserLoginInfo userLoginInfo = (UserLoginInfo)redisUtils.get(RedisConstants.USER_INFO_EXPIRE.getKey()+observeHasCreateInfo.getUserId());
            if(!"admin".equals(userLoginInfo.getUserDto().getLoginName())){
                Set<String> rolesSet =new HashSet<>(
                        userLoginInfo.getRoleDtoList().stream().map(RoleDto::getId).collect(Collectors.toList())
                );
                if(!rolesSet.contains(baseAllPermissionsRoleId)){
                    wrapper.eq("baseId",userLoginInfo.getUserDto().getBaseId());
                }
            }
        }
    }



    private void setCellValue(XSSFRow row, int i, String value) {
        row.createCell((short) i).setCellValue(value==null?"":value);
    }
}
